package com.example.city_defense.sprites;

import android.graphics.Matrix;
import com.badlogic.androidgames.framework.Pixmap;
import com.example.city_defense.Asset;
import com.example.city_defense.GameScreen;
import com.example.city_defense.Setting;
import com.example.city_defense.World;

public class Vehicle {

	public enum Direction {
		Up, Right, Down, Left
	};
	
	public enum VehicleState{
		Running , Explode , Finish
	};
	private World world;
	public float x, y;
	public int tileX;
	public int tileY;
	public float desX;
	public float desY;
	
	public int type;
	public int v;
	public Pixmap image;
	public int totalBlood;
	public int currentBlood;
	public int protect;
	public Direction currentDirection;
	
	public float tickTime;
	private boolean findDestination = false;
	public VehicleState state = VehicleState.Running;
	public int currentDegrees = 0;
	public int nextDegrees;
	public Matrix matrix;
	public int money = 10;
	private GameScreen gameScreen;
	

	public Vehicle( GameScreen gs ,World world, int type, float x, float y, int tileX,
			int tileY, float desX, float desY, boolean faster, boolean recovery) {
		// TODO Auto-generated constructor stub
		this.gameScreen = gs;
		this.currentDirection = Setting.initialDirection;
		this.x = x;
		this.y = y;
		this.type = type;
		this.world = world;
		matrix = new Matrix();
		this.tileX = tileX;
		this.tileY = tileY;
		this.desX = desX;
		this.desY = desY;
		this.v = 60;
		if (faster)
			v = (int) (v * 1.5);
		this.loadImage();
		this.currentBlood = totalBlood;
		if (this.currentDirection == Direction.Down) {
			matrix.postRotate(180, image.getWidth() / 2, image.getHeight() / 2);
			currentDegrees = 180;
		} else if (this.currentDirection == Direction.Right) {
			matrix.postRotate(90, image.getWidth() / 2, image.getHeight() / 2);
			currentDegrees = 90;
		} else if (this.currentDirection == Direction.Left) {
			matrix.postRotate(270, image.getWidth() / 2, image.getHeight() / 2);
			currentDegrees = 270;
		}
		nextDegrees = currentDegrees;

	}

	private void loadImage() {
		switch (type) {
		case 1:
			this.image = Asset.vehicle1;
			this.totalBlood = 12;
			this.protect = 0;
			break;
		case 2:
			this.image = Asset.vehicle2;
			this.totalBlood = 20;
			this.protect = 0;
			break;
		case 3:
			this.image = Asset.vehicle3;
			this.totalBlood = 30;
			this.protect = 0;
			break;
		case 4:
			this.image = Asset.vehicle4;
			this.totalBlood = 40;
			this.protect = 2;
			break;
		case 5:
			this.image = Asset.vehicle5;
			this.totalBlood = 45;
			this.protect = 2;
			break;
		case 6:
			this.image = Asset.vehicle6;
			this.totalBlood = 45;
			this.protect = 3;
			break;
		case 7:
			this.image = Asset.vehicle7;
			this.totalBlood = 45;
			this.protect = 5;
			break;
		case 8:
			this.image = Asset.vehicle8;
			this.totalBlood = 50;
			this.protect = 6;
			break;
		case 9:
			this.image = Asset.vehicle9;
			this.totalBlood = 50;
			this.protect = 7;
			break;
		case 10:
			this.image = Asset.vehicle10;
			this.totalBlood = 50;
			this.protect = 10;
			break;

		}
	}
	public void updateBlood(int attack){
		this.currentBlood -= (attack>this.protect)?(attack - this.protect):(0);
		if(this.currentBlood <= 0 ){
			this.state = VehicleState.Explode;
			for(Bullet bullet : gameScreen.listBullet){
				if(bullet.vehicle == this){
					bullet.vehicle =null;
				}
			}
		}
	}
	public void move(float deltatime) {

		if (this.state == VehicleState.Running) {
			if (currentDirection == Direction.Up) {
				// Log.i("Up", "x= " + this.x + "  " + "y = " + this.y);
				float temp = y - v * deltatime;
				if (temp <= desY) {
					if (findDestination == false) {
						y = desY;
						findPatch();
					} else {
						this.state = VehicleState.Finish;
					}
				} else {
					y = temp;
				}
			} else if (currentDirection == Direction.Right) {
				// Log.i("Right", "x= " + this.x + "  " + "y = " + this.y);
				float temp = x + v * deltatime;
				if (temp >= desX) {
					if (!findDestination) {
						x = desX;
						findPatch();
					} else {
						this.state = VehicleState.Finish;
					}
				} else {
					x = temp;
				}
			} else if (currentDirection == Direction.Down) {
				// Log.i("Down", "x= " + this.x + "  " + "y = " + this.y);
				float temp = y + v * deltatime;
				if (temp >= desY) {
					if (!findDestination) {
						y = desY;
						findPatch();
					} else {
						this.state = VehicleState.Finish;
					}
				} else {
					y = temp;
				}
			} else if (currentDirection == Direction.Left) {
				// Log.i("LEFT", "x= " + this.x + "  " + "y = " + this.y);
				float temp = x - v * deltatime;
				if (temp <= desX) {
					if (!findDestination) {
						x = desX;
						findPatch();
					} else {
						this.state = VehicleState.Finish;
					}
				} else {
					x = temp;
				}
			}
			// Log.i("", "x= " + this.x + "  " + "y = " + this.y);
		}
	}

	public void findPatch() {
		// Log.i("Find Patch", "x= " + this.x + "  " + "y = " + this.y);
		if (currentDirection == Direction.Up) {
			// Matrix rotate 0 degrees
			if (world.tileMap[tileY - 1][tileX] == 3) {
				tileY = tileY - 1;
				desY = desY - 50;
			} else if (world.tileMap[tileY][tileX + 1] == 3) {
				tileX = tileX + 1;
				desX = desX + 50;
				matrix.postRotate(90, image.getWidth(), image.getHeight());
				this.currentDirection = Direction.Right;
			} else if (world.tileMap[tileY][tileX - 1] == 3) {
				tileX = tileX - 1;
				desX = desX - 50;
				this.currentDirection = Direction.Left;
				matrix.postRotate(-90, image.getWidth(), image.getHeight());
			} else {
				tileY = tileY + 1;
				desY = desY + 50;
				this.currentDirection = Direction.Down;
				matrix.postRotate(180, image.getWidth(), image.getHeight());
			}
		} else if (currentDirection == Direction.Right) {
			// Matrix rotate 90 degrees
			if (world.tileMap[tileY][tileX + 1] == 3) {
				tileX = tileX + 1;
				desX = desX + 50;
			} else if (world.tileMap[tileY + 1][tileX] == 3) {
				tileY = tileY + 1;
				desY = desY + 50;
				this.currentDirection = Direction.Down;
				matrix.postRotate(90, image.getWidth(), image.getHeight());
			} else if (world.tileMap[tileY - 1][tileX] == 3) {
				tileY = tileY - 1;
				desY = desY - 50;
				this.currentDirection = Direction.Up;
				matrix.postRotate(-90, image.getWidth(), image.getHeight());
			} else {
				tileX = tileX - 1;
				desX = desX - 50;
				this.currentDirection = Direction.Left;
				matrix.postRotate(180, image.getWidth(), image.getHeight());
			}
		} else if (currentDirection == Direction.Down) {
			// Matrix rotate 180 degrees
			if (world.tileMap[tileY + 1][tileX] == 3) {
				tileY = tileY + 1;
				desY = desY + 50;
			} else if (world.tileMap[tileY + 1][tileX] == 2) {
				desY += 100;
				findDestination = true;
			} else if (world.tileMap[tileY][tileX + 1] == 3) {
				tileX = tileX + 1;
				desX = desX + 50;
				this.currentDirection = Direction.Right;
				matrix.postRotate(-90, image.getWidth(), image.getHeight());
			} else if (world.tileMap[tileY][tileX - 1] == 3) {
				tileX = tileX - 1;
				desX = desX - 50;
				this.currentDirection = Direction.Left;
				matrix.postRotate(90, image.getWidth(), image.getHeight());
			} else {
				tileY = tileY - 1;
				desY = desY - 50;
				this.currentDirection = Direction.Up;
				matrix.postRotate(180, image.getWidth(), image.getHeight());
			}
		} else if (currentDirection == Direction.Left) {
			// Matrix rotate -90 degrees
			if (world.tileMap[tileY][tileX - 1] == 3) {
				tileX = tileX - 1;
				desX = desX - 50;
			} else if (world.tileMap[tileY][tileX - 1] == 2) {
				desX = desX - 100;
				findDestination = true;
			} else if (world.tileMap[tileY + 1][tileX] == 3) {
				tileY = tileY + 1;
				desY = desY + 50;
				this.currentDirection = Direction.Down;
				matrix.postRotate(-90, image.getWidth(), image.getHeight());
			} else if (world.tileMap[tileY - 1][tileX] == 3) {
				tileY = tileY - 1;
				desY = desY - 50;
				this.currentDirection = Direction.Up;
				matrix.postRotate(90, image.getWidth(), image.getHeight());
			} else {
				tileX = tileX + 1;
				desX = desX + 50;
				this.currentDirection = Direction.Right;
				matrix.postRotate(180, image.getWidth(), image.getHeight());
			}
		}
	}
}
